package com.mdb.open.epay.service.epay.handle;

import cn.hutool.core.date.DatePattern;
import com.mdb.bc.api.data.ResultEx;
import com.mdb.bc.utils.ObjectUtil;
import com.mdb.open.epay.config.EPayOpenProperties;
import com.mdb.open.epay.feign.OutreachClient;
import com.mdb.open.epay.feign.dto.*;
import com.mdb.open.epay.service.epay.handle.dto.CoreCancelDto;
import com.mdb.open.epay.service.epay.handle.dto.CoreTradeDto;
import com.mdb.open.epay.service.epay.handle.vo.CoreCancelVo;
import com.mdb.open.epay.service.epay.handle.vo.CoreTradeVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Log4j2
@Service
public class CoreTradeHandle {

    @Autowired
    private OutreachClient outreachClient;

    @Autowired
    private EPayOpenProperties ePayOpenProperties;

    /**
     * 0351430-一对一公共记账接口
     */
    public ResultEx<CoreTradeVo> coreTrade(CoreTradeDto dto, String innerOrderNo, String remark) {
        try {
            HST_0351430 requestDto = new HST_0351430();

            HSTHeadSimpleDto head = new HSTHeadSimpleDto();
            head.setReqRef(innerOrderNo);
            head.setTrId("0351430");
            requestDto.setHead(head);

            HST_0351430_Req hst_0351430_req = new HST_0351430_Req();
            hst_0351430_req.setValDt(dto.getValDt());
            hst_0351430_req.setCcy(dto.getCcy());
            hst_0351430_req.setAmt(dto.getAmt());
            hst_0351430_req.setDacTyp(dto.getDacTyp());
            hst_0351430_req.setDrAc(dto.getDrAc());
            hst_0351430_req.setDrCcy(dto.getDrCcy());
            hst_0351430_req.setDrBr(dto.getDrBr());
            hst_0351430_req.setChqNo(dto.getChqNo());
            hst_0351430_req.setChqIsdt(dto.getChqIsdt());
            hst_0351430_req.setHolNo(dto.getHolNo());
            hst_0351430_req.setCacTyp(dto.getCacTyp());
            hst_0351430_req.setCrAc(dto.getCrAc());
            hst_0351430_req.setCrNm(dto.getCrNm());
            hst_0351430_req.setCrCcy(dto.getCrCcy());
            hst_0351430_req.setCrBr(dto.getCrBr());
            hst_0351430_req.setFeeCd1(dto.getFeeCd1());
            hst_0351430_req.setFeeAct1(dto.getFeeAct1());
            hst_0351430_req.setFeeAc1(dto.getFeeAc1());
            hst_0351430_req.setFeeBr1(dto.getFeeBr1());
            hst_0351430_req.setFeeCcy1(dto.getFeeCcy1());
            hst_0351430_req.setFeeAmt1(dto.getFeeAmt1());
            hst_0351430_req.setFeeCd2(dto.getFeeCd2());
            hst_0351430_req.setFeeAct2(dto.getFeeAct2());
            hst_0351430_req.setFeeAc2(dto.getFeeAc2());
            hst_0351430_req.setFeeBr2(dto.getFeeBr2());
            hst_0351430_req.setFeeCcy2(dto.getFeeCcy2());
            hst_0351430_req.setFeeAmt2(dto.getFeeAmt2());
            hst_0351430_req.setFeeCd3(dto.getFeeCd3());
            hst_0351430_req.setFeeAct3(dto.getFeeAct3());
            hst_0351430_req.setFeeAc3(dto.getFeeAc3());
            hst_0351430_req.setFeeBr3(dto.getFeeBr3());
            hst_0351430_req.setFeeCcy3(dto.getFeeCcy3());
            hst_0351430_req.setFeeAmt3(dto.getFeeAmt3());
            hst_0351430_req.setRemark(remark);
            hst_0351430_req.setNarr(ePayOpenProperties.getCoreTranNarrtive());
            hst_0351430_req.setOthAc(dto.getOthAc());
            hst_0351430_req.setOthNm(dto.getOthNm());
            hst_0351430_req.setOthBk(dto.getOthBk());
            requestDto.setBody(hst_0351430_req);

            ResultEx<HST_0351430_Resp> coreResultEx = outreachClient.requestCoreApi0351430(requestDto);
            if (coreResultEx.isFailed()) {
                return ResultEx.makeResult(coreResultEx.getRetCode(), "核心失敗");
            }

            if (ObjectUtil.isEmpty(coreResultEx.getData())) {
                return ResultEx.makeResult(coreResultEx.getRetCode(), "核心結果為空");
            }

            HST_0351430_Resp hst_0351430_resp = coreResultEx.getData();
            CoreTradeVo coreTradeVo = new CoreTradeVo();
            coreTradeVo.setTSC01(hst_0351430_resp.getTSC01());
            coreTradeVo.setCMG26(hst_0351430_resp.getCMG26());
            LocalDate transDate = LocalDate.parse(hst_0351430_resp.getTradeDate(), DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN));
            coreTradeVo.setTradeDate(transDate.atStartOfDay());
            return ResultEx.ok(coreTradeVo);
        } catch (Exception exception) {
            log.error("调用外联平台发生异常。异常信息:{}", exception.getMessage());
            return ResultEx.fail();
        }
    }


    /**
     * 核心沖正
     */
    public ResultEx<CoreCancelVo> coreCancel(CoreCancelDto dto, String innerOrderNo) {
        try {
            HST_9998889 requestDto = new HST_9998889();

            HSTHeadSimpleDto head = new HSTHeadSimpleDto();
            head.setReqRef(innerOrderNo);
            head.setTrId("9998889");
            requestDto.setHead(head);

            HST_9998889_Req hst_9998889_req = new HST_9998889_Req();
            hst_9998889_req.setFlg(dto.getFlg());
            hst_9998889_req.setCoreDt(dto.getCoreDt());
            hst_9998889_req.setCoreJrn(dto.getCoreJrn());
            hst_9998889_req.setExSys(dto.getExSys());
            hst_9998889_req.setExJrn(dto.getExJrn());
            hst_9998889_req.setStmInd(dto.getStmInd());
            hst_9998889_req.setServId(dto.getServId());
            hst_9998889_req.setVchNo(dto.getVchNo());
            hst_9998889_req.setResn(dto.getResn());
            requestDto.setBody(hst_9998889_req);
            ResultEx<HST_9998889_Resp> coreResultEx = outreachClient.requestCoreApi9998889(requestDto);
            HST_9998889_Resp resp = coreResultEx.getData();
            if (coreResultEx.isFailed()) {
                return ResultEx.makeResult(coreResultEx.getRetCode(),"核心失敗");
            }

            if (ObjectUtil.isEmpty(resp)) {
                return ResultEx.makeResult(coreResultEx.getRetCode(), "核心結果為空");
            }
            CoreCancelVo coreCancelVo = new CoreCancelVo();
            LocalDate transDate = LocalDate.parse(resp.getCoreTrDt(), DateTimeFormatter.ofPattern(DatePattern.PURE_DATE_PATTERN));
            coreCancelVo.setTransDate(transDate.atStartOfDay());
            coreCancelVo.setCoreJrnno(resp.getCoreJrnno());
            return ResultEx.ok(coreCancelVo);
        } catch (Exception ex) {
            log.error("调用外联平台沖正发生异常。异常信息:{}", ex.getMessage());
            return ResultEx.fail();
        }
    }
}
